package com.xunua.gaud_map_demo;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.amap.api.navi.enums.MotionAction;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.ObservableEmitter;
import io.reactivex.rxjava3.core.ObservableOnSubscribe;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.functions.Predicate;
import io.reactivex.rxjava3.schedulers.Schedulers;

public class BannerActivity extends AppCompatActivity {
    @BindView(R.id.banner_ViewPager)
    ViewPager viewPager;
    @BindView(R.id.first_rb01)
    RadioButton firstRb01;
    @BindView(R.id.first_rb02)
    RadioButton firstRb02;
    @BindView(R.id.first_rb03)
    RadioButton firstRb03;
    @BindView(R.id.first_rb04)
    RadioButton firstRb04;
    @BindView(R.id.first_rg01)
    RadioGroup firstRg01;

    private List<RadioButton> rbs;

    private List<View> mData;

    private Boolean ViewPagerAutoNext=true;
    private Boolean isDesotry=false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_banner);
        ButterKnife.bind(this);
        initViewPager();
    }

    private void initViewPager() {
        rbs=new ArrayList<>();
        rbs.add(firstRb01);
        rbs.add(firstRb02);
        rbs.add(firstRb03);
        rbs.add(firstRb04);
        mData = new ArrayList<>();
        mData.add(LayoutInflater.from(this).inflate(R.layout.viewpager01,null));
        mData.add(LayoutInflater.from(this).inflate(R.layout.viewpager02,null));
        mData.add(LayoutInflater.from(this).inflate(R.layout.viewpager03,null));
        mData.add(LayoutInflater.from(this).inflate(R.layout.viewpager04,null));
        viewPager.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                return Integer.MAX_VALUE;
            }

            @Override
            public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
                return view==object;
            }

            @NonNull
            @Override
            public Object instantiateItem(@NonNull ViewGroup container, int position) {
                ViewGroup parent = (ViewGroup) mData.get(position % rbs.size()).getParent();
                if (parent != null) {
                    parent.removeAllViews();
                }
                mData=new ArrayList<>();
                mData.add(getLayoutInflater().inflate(R.layout.viewpager01,null));
                mData.add(getLayoutInflater().inflate(R.layout.viewpager02,null));
                mData.add(getLayoutInflater().inflate(R.layout.viewpager03,null));
                mData.add(getLayoutInflater().inflate(R.layout.viewpager04,null));
                container.addView(mData.get(position%rbs.size()));
                return mData.get(position%mData.size());
            }

            @Override
            public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
                View view = mData.get(position % mData.size());
                container.removeView(view);
            }
        });

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                if (positionOffset != 0) {
                    ViewPagerAutoNext=false;
                }else {
                    ViewPagerAutoNext=true;
                }
                rbs.get(position%rbs.size()).setChecked(true);
            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        viewPager.setCurrentItem(rbs.size()*1000);

        viewPager.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionAction.ACTION_MOVE||event.getAction()==MotionEvent.ACTION_DOWN) {
                    ViewPagerAutoNext=false;
                }else if (event.getAction()==MotionEvent.ACTION_UP){
                    ViewPagerAutoNext=true;
                }
                return false;
            }
        });

        Observable.interval(3000,3000, TimeUnit.MILLISECONDS)
                .subscribeOn(Schedulers.newThread())
                .takeWhile(new Predicate<Long>() {
                    @Override
                    public boolean test(Long aLong) throws Throwable {
                        return !isDesotry;
                    }
                }).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Long>() {
                    @Override
                    public void onSubscribe(@io.reactivex.rxjava3.annotations.NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@io.reactivex.rxjava3.annotations.NonNull Long aLong) {
                        if (ViewPagerAutoNext) {
                            Log.e("TAG", "onNext: "+aLong );
                            viewPager.setCurrentItem(viewPager.getCurrentItem()+1);
                        }
                    }

                    @Override
                    public void onError(@io.reactivex.rxjava3.annotations.NonNull Throwable e) {

                    }

                    @Override
                    public void onComplete() {
                        Log.e("TAG", "onComplete: 完成了" );
                    }
                });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        isDesotry=true;
    }

    @Override
    protected void onStart() {
        super.onStart();
        ViewPagerAutoNext=true;
    }

    @Override
    protected void onStop() {
        super.onStop();
        ViewPagerAutoNext=false;
    }
}
